﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KeyboardImageViewer
{
    /// <summary>
    /// Interaction logic for EditTagDependencies.xaml
    /// </summary>
    public partial class EditTagDependencies : Window
    {
        private ImageManager manager;
        private List<KeyValuePair<string, string>> dependencyList;

        public EditTagDependencies(ImageManager manager, MainWindow window)
        {
            this.manager = manager;

            this.Owner = window;

            InitializeComponent();

            this.dependencyList = new List<KeyValuePair<string, string>>();
            this.dependencyList.AddRange(this.manager.DependencyList);

            foreach (KeyValuePair<string, string> dependency in this.manager.DependencyList)
            {
                this.dependencyListBox.Items.Add(dependency.Key + " -> " + dependency.Value);
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.tagBox.Text.Trim().Length == 0 || this.dependentTagBox.Text.Trim().Length == 0)
            {
                MessageBox.Show("Cannot add dependency, fields cannot be blank.");
                return;
            }

            string tag = this.tagBox.Text.ToLowerInvariant();
            string dependency = this.dependentTagBox.Text.ToLowerInvariant();

            this.dependencyList.Add(new KeyValuePair<string, string>(tag, dependency));
            this.dependencyListBox.Items.Add(tag + " -> " + dependency);

            this.tagBox.Text = "";
            this.dependentTagBox.Text = "";
        }

        private void removeButton_Click(object sender, RoutedEventArgs e)
        {
            int removeIndex = this.dependencyListBox.SelectedIndex;

            if (removeIndex >= 0)
            {
                this.dependencyList.RemoveAt(this.dependencyListBox.SelectedIndex);
                this.dependencyListBox.Items.RemoveAt(this.dependencyListBox.SelectedIndex);
            }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            this.manager.DependencyList = this.dependencyList;
            this.DialogResult = true;
        }
    }
}
